package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
@Slf4j
@CrossOrigin
public class UserController {

    /**
     * 注意此处的注解是dubbo注解
     */
    @DubboReference
    private UserService userService;


    /**
     * @Author: Administrator
     * @Date: 18:08 2021/3/11
     * @Parms [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 删除用户
     */
    @GetMapping("/delete")
    public Result delete(Integer id) {
        log.info("[删除----前端id参数]data:{}", id);
        userService.delete(id);
        return new Result(true, "删除用户成功");
    }

    /**
     * @Author: jay
     * @Date: 14:36 2021/3/12
     * @Params [roleIds, user]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 用户-修改
     */
    @PostMapping("edit")
    public Result edit(Integer[] roleIds, @RequestBody com.itheima.health.pojo.User user) {
        log.info("[用户-修改] data:{},roleIds:{}", user, roleIds);
        userService.edit(user, roleIds);
        return new Result(true, "用户修改成功");
    }

    /**
     * @Author: Administrator
     * @Date: 18:08 2021/3/11
     * @Parms [RolesIds, user]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 添加用户
     */
    @PostMapping("add")
    //@PreAuthorize("hasAnyAuthority('USER_ADD')")
    public Result add(Integer[] roleIds, @RequestBody  com.itheima.health.pojo.User user) {
        log.info("[前端RolesIds==]RolesIds:{}", roleIds);
        userService.add(roleIds, user);
        return new Result(true, "添加成功");
    }

    /**
     * @Author: Administrator
     * @Date: 10:35 2021/3/11
     * @Parms [queryPageBean]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 用户数据查询
     */
    @GetMapping("findPage")
    //@PreAuthorize("hasAnyAuthority('USER_QUERY')")
    public Result findPage(QueryPageBean queryPageBean) {
        PageResult pageResult = userService.findPage(queryPageBean);
        return new Result(true, "查询用户成功", pageResult);
    }

    /**
     * @Author: guodong
     * @Date: 11:13 2021/2/23
     * @Parms [loginParam]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 用户登录
     */
    /*@PostMapping("login")
    public Result login(@RequestBody LoginParam loginParam) {
        //1.根据用户名查询用户
        System.out.println(loginParam.getUsername());
        User user = userService.findUserByName(loginParam.getUsername());
        //2.判断用户是否为null 或者 用户密码是否一致 如果两者有其一不确立那么返回登录失败
        if (null == user || !user.getPassword().equals(loginParam.getPassword())) {
            //return new Result(false, "用户名或密码错误");
            return new Result(false, MessageConst.LOGIN_FAIL);
        }
        //3.登录成功
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }*/
    @PostMapping("loginsuccess")
    public Result loginsuccess() {
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }

    @PostMapping("loginfailure")
    public Result loginfailure() {
        return new Result(true, MessageConst.LOGIN_FAIL);
    }

    @GetMapping("getusername")
    public Result getusername() {
        log.info("[获取登录用户名称]");
        Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (obj instanceof User) {
            User user = (User) obj;
            return new Result(true, MessageConst.GET_USERNAME_SUCCESS, user.getUsername());
        }
        return new Result(false, MessageConst.GET_USERNAME_FAIL);
    }

    /**
     * @Author: lenovo
     * @Date: 9:25 2021/3/10
     * @Params []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 获取登录用户菜单
     */
    @GetMapping("getMenuList")
    public Result getMenuList() {
        log.info("[用户-菜单查询]");
        Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (obj instanceof User) {
            User user = (User) obj;
            List<Menu> menuList = userService.getMenuList(user.getUsername());
            return new Result(true, MessageConst.GET_MENU_SUCCESS, menuList);
        }
        return new Result(false, MessageConst.GET_MENU_FAIL);
    }


    /**
     * @Author: jay
     * @Date: 12:25 2021/3/12
     * @Params [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 用户-根据id查询
     */
    @GetMapping("findById")
    public Result findById(Integer id) {
        log.info("[用户-根据id查询] data:{}", id);
        com.itheima.health.pojo.User user = userService.findById(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, user);
    }

    /**
     * @Author: jay
     * @Date: 12:29 2021/3/12
     * @Params [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 用户-查询角色id
     */
    @GetMapping("findRoleIdsById")
    public Result findRoleIdsById(Integer id) {
        log.info("[用户-查询角色id] data:{}", id);
        List<Integer> roleIds = userService.findRoleIdsById(id);
        log.info("[用户-查询角色id-------------] data:{}", roleIds);
        return new Result(true, MessageConst.ACTION_SUCCESS, roleIds);
    }


}
